<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Entity_Session_Admin extends Core_Entity {

	const ERROR_MSG = 1;
	const SUCCESS_MSG = 2;
	
	public function getMessages($clear=false) {
		$messages = App::session('messages');
		if (!is_array($messages)) $messages = array();
		if ($clear) App::session('messages', null);
		return $messages;
	}

	public function addError($e) {
		if (is_array($e)) {
			foreach($e as $msg) {
				$this->_addMessage(self::ERROR_MSG, $msg);
			}
		} elseif (is_string($e) && !empty($e)) {
			$this->_addMessage(self::ERROR_MSG, $e);
		}
	}
	
	public function addSuccess($s) {
		$this->_addMessage(self::SUCCESS_MSG, $s);
	}
	
	protected function _addMessage($type, $s) {
		$messages = $this->getMessages();
		array_push($messages, array('type' => $type, 'msg' => $s));
		App::session('messages', $messages);
	}
	
	public function setFormData($val) {
		App::session('form_data', $val);
		return $this;
	}

	public function getFormData($clear=false) {
		$result = App::session('form_data');
		if ($clear) App::session('form_data', null);
		return $result;;
	}
	
}